// pages/genjin2/genjin2.js

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    customer_id:"",
    desc:"",
    image:"",
    time:"",
    follow_status:"",
    list: [],
    select:[]//最后选择的数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      customer_id: options.customer_id,
      desc: options.desc,
      image: options.image,
      time: options.time,
      follow_status: options.follow_status,
    })

    var that = this
    wx.request({
      url: app.globalData.httpurl +'v2/customerSign',
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data:{
      },
      method:"GET",
      success:function(res){
        var datas= res.data.data
        for (var i = 0; i < datas.length; i++) {
          datas[i].select = 0
        }
        that.setData({
          list: datas
        })
      }
    })

   
  },

  bindselect:function(e){
    var id = e.currentTarget.dataset.id    

    for (var i = 0; i < this.data.list.length; i++) {
      if (this.data.list[i].id == id){
        if (this.data.list[i].select == 0){
          this.data.list[i].select = 1
        }else{
          this.data.list[i].select = 0
        }
      }
    }
    this.setData({
      list: this.data.list
    })
  },
  bindsub: function(){
    this.setData({
      select: []
    })
    for (var i = 0; i < this.data.list.length; i++) {
      if (this.data.list[i].select == 1) {
        this.setData({
          select: this.data.select.concat(this.data.list[i].id)
        })
      }
    }

    var that = this
    wx.request({
      url: app.globalData.httpurl +'v2/follow/create',
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data:{
        "customer_id": that.data.customer_id,
        "desc": that.data.desc,
        "image": that.data.image,
        "time": that.data.time,
        "follow_status": that.data.follow_status,
        "sign": that.data.select.join(",")
      },
      method:"POST",
      success:function(res){
        if(res.data.status == 1){
          wx.navigateBack({
            delta: 2
          })
          // wx.redirectTo({
          //   url: '/pages/item/item?id=' + that.data.customer_id,
          // })
        }
      }
    })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})